package net.yunzhanyi.client.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.yunzhanyi.client.domain.dto.CollectionDto;
import net.yunzhanyi.client.domain.vo.PageVo;
import net.yunzhanyi.client.service.CollectionService;
import net.yunzhanyi.client.utils.PageUtils;
import net.yunzhanyi.generic.domain.model.ClientCollection;
import net.yunzhanyi.generic.mapper.AuthorMapper;
import net.yunzhanyi.generic.mapper.ClientCollectionMapper;
import net.yunzhanyi.generic.mapper.PoetryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bestct
 * @date 2022/10/3
 * @type 类
 */
@Service
public class CollectionServiceImpl implements CollectionService {

    @Autowired
    private ClientCollectionMapper collectionMapper;

    @Autowired
    private PoetryMapper poetryMapper;

    @Autowired
    private AuthorMapper authorMapper;

    @Override
    public void addCollection(Long resId, long uid, Integer type) {
        ClientCollection collection = collectionMapper.selectByResIdAndUid(uid, resId, type);
        if (collection == null) {
            collection = new ClientCollection();
            collection.setCollectionTime(System.currentTimeMillis());
            collection.setUserId(uid);
            collection.setResId(resId);
            collection.setCollectionType(type);
            collection.setCollectionStatus(1);
            collectionMapper.insertSelective(collection);
        } else {
            if (collection.getCollectionStatus() == 1) {
                throw new RuntimeException("收藏失败,原因:已收藏");
            } else {
                collection.setCollectionStatus(1);
                collection.setCollectionTime(System.currentTimeMillis());
                collectionMapper.updateByPrimaryKeySelective(collection);
            }
        }
    }

    @Override
    public void cancelCollection(Long resId, long uid, Integer type) {
        ClientCollection collection = collectionMapper.selectByResIdAndUid(uid, resId, type);
        if (collection == null) {
            throw new RuntimeException("未收藏");
        } else {
            if (collection.getCollectionStatus() == 0) {
                throw new RuntimeException("未收藏");
            } else {
                collection.setCollectionStatus(0);
                collectionMapper.updateByPrimaryKeySelective(collection);
            }
        }
    }

    @Override
    public Boolean isCollected(long uid, Long poetryId, int i) {

        if (uid == -1) {
            return false;
        }
        ClientCollection collection = collectionMapper.selectByResIdAndUid(uid, poetryId, i);
        if (collection == null) {
            return false;
        }

        return collection.getCollectionStatus() == 1;
    }

    @Override
    public PageVo<CollectionDto> getUserCollection(Long uid, Integer type, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CollectionDto> collectionDtos=new ArrayList<>();
        List<ClientCollection> collectionList = collectionMapper.selectByUid(uid, type);
        for (ClientCollection collection : collectionList) {
            String name = null;
            if (collection.getCollectionType() == 1) {
                name = poetryMapper.selectTitleById(collection.getResId());
            } else if (collection.getCollectionType() == 2) {
                name = authorMapper.selectAuthorNameById(collection.getResId());
            }
            CollectionDto collectionDto = new CollectionDto();
            BeanUtils.copyProperties(collection,collectionDto);
            collectionDto.setResName(name);
            collectionDtos.add(collectionDto);
        }
        PageInfo<ClientCollection> collectionPageInfo = new PageInfo<>(collectionList);
        PageVo<CollectionDto> collectionDtoPageVo = new PageVo<>(collectionDtos);
        PageUtils.copyPageInfo(collectionPageInfo,collectionDtoPageVo);
        return collectionDtoPageVo;
    }

    @Override
    public void cancelCollectionById(Long colId) {
        collectionMapper.updateStatusByPrimaryKey(colId,0);
    }

    @Override
    public void reCollectionById(Long colId) {
        collectionMapper.updateStatusByPrimaryKey(colId,1);
    }


}
